package pers.julio.notepad.PageSwitcher.Base;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;

import androidx.fragment.app.Fragment;
import pers.julio.notepad.PageSwitcher.Bean.PageAnim;
import pers.julio.notepad.PageSwitcher.Bean.SwitchBean;
import pers.julio.notepad.PageSwitcher.IPageSwitcher;

/**
 * ClassName:  BaseFragment
 * Description: <全局基类BaseFragment>
 * Author: julio_chan  2020/10/20 11:45
 */
public class BaseFragment extends Fragment {
    private static final String TAG = BaseFragment.class.getSimpleName();

    protected Activity mActivity;    //所在activity

    private String mPageName;    //页面名
    public String getPageName() { return mPageName; }
    public void setPageName(String pageName) {
        mPageName = pageName;
    }

    private int mRequestCode;  //用于startForResult的requestCode
    public int getRequestCode() { return mRequestCode; }
    public void setRequestCode(int code) { mRequestCode = code; }

    //openPageForResult接口，用于传递返回结果
    private OnFragmentFinishListener mFragmentFinishListener;
    public void setFragmentFinishListener(OnFragmentFinishListener listener) { mFragmentFinishListener = listener; }
    public void setFragmentResult(int resultCode, Intent intent) {
        if (mFragmentFinishListener != null) {
            mFragmentFinishListener.onFragmentResult(mRequestCode, resultCode, intent);
        }
    }
    private IPageSwitcher mPageSwitcher;
    public void setSwitcher(IPageSwitcher pageSwitcher) { mPageSwitcher = pageSwitcher; }
    // 得到页面切换Switcher
    public IPageSwitcher getSwitcher() {
        synchronized (BaseFragment.this) {// 加强保护，保证pageSwitcher 不为null
            if (mPageSwitcher == null) {
                if (mActivity != null && mActivity instanceof IPageSwitcher) {
                    mPageSwitcher = (IPageSwitcher) mActivity;
                }
                if (mPageSwitcher == null) {
                    BaseActivity topActivity = BaseActivity.getTopActivity();
                    if (topActivity != null && topActivity instanceof IPageSwitcher) {
                        mPageSwitcher = topActivity;
                    }
                }
            }
        }
        return mPageSwitcher;
    }


    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        mActivity = activity;
    }
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getPageName() != null) {
            Log.d(TAG, "====Fragment.onCreate====" + getPageName());
        }
    }

    //查找fragment是否存在，通过Switcher查找
    public boolean findPage(String pageName) {
        if (pageName == null || "".equals(pageName)) { return false; }

        IPageSwitcher IPageSwitcher = getSwitcher();
        if (IPageSwitcher != null) {
            return IPageSwitcher.findPage(pageName);
        } else {
            return false;
        }
    }

    // 对应fragment是否位于栈顶，通过Switcher查找
    public boolean isFragmentTop(String fragmentTag) {
        IPageSwitcher pageIPageSwitcher = this.getSwitcher();
        if (pageIPageSwitcher != null) {
            return pageIPageSwitcher.isFragmentTop(fragmentTag);
        } else {
            Log.d(TAG, "pageSwitcher is null");
            return false;
        }
    }

    //数据设置，回调
    public void onFragmentDataReset(Bundle bundle) { }
    // 重写该方法用于获得返回的数据
    public void onFragmentResult(int requestCode, int resultCode, Intent data) {
        Log.d(TAG, "onFragmentResult from baseFragment：requestCode-" + requestCode + "  resultCode-" + resultCode);
    }

    // 在Fragment中打开页面的方法
    public final Fragment openPage(String pageName, Bundle bundle, int[] anim, boolean addToBackStack, boolean newActivity) {
        if (pageName == null || "".equals(pageName)) { return null; }
        IPageSwitcher IPageSwitcher = this.getSwitcher();
        if (IPageSwitcher != null) {
            SwitchBean page = new SwitchBean(pageName, bundle, anim, addToBackStack, newActivity);
            return IPageSwitcher.openPage(page);
        } else {
            Log.d(TAG, "pageSwitcher is null");
            return null;
        }
    }

    public final Fragment openPage(String pageName, Bundle bundle, PageAnim pageAnim) {
        return openPage(pageName, bundle, SwitchBean.convertAnimations(pageAnim), true);
    }
    public final Fragment openPage(String pageName, Bundle bundle, int[] anim, boolean addToBackStack) {
        return openPage(pageName, bundle, anim, addToBackStack, false);
    }
    public final Fragment openPage(String pageName, Bundle bundle, int[] anim) {
        return openPage(pageName, bundle, anim, true);
    }
    public final Fragment openPage(String pageName, Bundle bundle, PageAnim pageAnim, boolean addToBackStack) {
        return openPage(pageName, bundle, SwitchBean.convertAnimations(pageAnim), addToBackStack, false);
    }
    public final Fragment openPage(String pageName, Bundle bundle, PageAnim pageAnim, boolean addToBackStack, boolean newActivity) {
        return openPage(pageName, bundle, SwitchBean.convertAnimations(pageAnim), addToBackStack, newActivity);
    }

    // 在Fragment中切换页面的方法
    public Fragment gotoPage(String pageName, Bundle bundle, PageAnim pageAnim, boolean newActivity) {
        IPageSwitcher pageSwitcher = this.getSwitcher();
        if (pageSwitcher != null) {
            SwitchBean page = new SwitchBean(pageName, bundle, pageAnim, true, newActivity);
            return pageSwitcher.switchPage(page);
        } else {
            return null;
        }
    }
    public Fragment gotoPage(String pageName, Bundle bundle, PageAnim pageAnim) {
        return gotoPage(pageName, bundle, pageAnim,false);
    }

    // 打开fragment并请求获得返回值,并设置是否在新activity中打开
    public final Fragment openPageForResult(boolean newActivity, String pageName, Bundle bundle, PageAnim pageAnim, int requestCode) {
        IPageSwitcher pageSwitcher = this.getSwitcher();
        if (pageSwitcher != null) {
            SwitchBean switcher = new SwitchBean(pageName, bundle, pageAnim, true, newActivity);
            switcher.setmRequestCode(requestCode);
            return pageSwitcher.openPageForResult(switcher, this);
        } else {
            return null;
        }
    }
    public final Fragment openPageForResult(String pageName, Bundle bundle, PageAnim pageAnim, int requestCode) {
        return openPageForResult(false, pageName, bundle, pageAnim, requestCode);
    }

    //弹出栈顶的Fragment。如果Activity中只有一个Fragemnt时，Acitivity也退出。
    public void popToBack() {
        popToBack(null, null);
    }
    //如果在fragment栈中找到，则跳转到该fragment中去，否则弹出栈顶
    public final void popToBack(String pageName, Bundle bundle) {
        IPageSwitcher IPageSwitcher = getSwitcher();
        if (IPageSwitcher != null) {
            if (pageName == null) {
                IPageSwitcher.popPage();
            } else {
                if (this.findPage(pageName)) {
                    SwitchBean page = new SwitchBean(pageName, bundle);
                    IPageSwitcher.switchPage(page);
                } else {
                    IPageSwitcher.popPage();
                }
            }
        } else {
            Log.d(TAG, "pageSwitcher null");
        }
    }


    /** 将Activity中onKeyDown在Fragment中实现，*/
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        return false;
    }


    @Override
    public void onDetach() {
        super.onDetach();
        mActivity = null;
    }

    /**
     * 用于openPageForResult，获得返回内容后需要再次调openPage的场景，只适合不新开Activity的情况，如果新开activity请像Activity返回结果那样操作
     * @param callback 返回码
     */
    public void popToBackForResult(PopCallback callback) {
        this.popToBack(null, null);
        callback.run();
    }


    public interface PopCallback{
        void run();
    }
    /*页面跳转接口 */
    public interface OnFragmentFinishListener {
        void onFragmentResult(int requestCode, int resultCode, Intent intent);
    }

}
